package com.yang.dao;

import com.yang.entity.NoteUser;
import com.yang.entity.PageInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户表(NoteUser)表数据库访问层
 *
 * @author yangdacheng
 * @since 2022-03-02 19:11:03
 */
@Mapper
@Repository
public interface NoteUserDao {

    /**
     * 添加一个新的用户
     * @param noteUser
     * @return
     */
    @Insert("INSERT INTO noteUser VALUES(" +
            "#{userid},#{name},#{userImg},#{username},#{password},#{userTel}," +
            "#{userEmail},#{roleId})")
    Boolean insertUser(NoteUser noteUser);

    /**
     * 更新用户信息
     * @param noteUser
     * @return
     */
    @Update("UPDATE noteUser SET name=#{name},userImg=#{userImg},password=#{password}," +
            "userTel=#{userTel},userEmail=#{userEmail},roleId=#{roleId} WHERE " +
            "userID=#{userid}")
    Boolean updateUser(NoteUser noteUser);

    /**
     * 删除用户信息
     * @param userID
     * @return
     */
    @Delete("DELETE FROM noteUser WHERE userID=#{userID}")
    Boolean deleteUser(String userID);

    /**
     * 查询所有的用户
     * @return
     */
    @Results(value = {
            @Result(column = "userID",property = "userid",id = true),
            @Result(column = "name",property = "name"),
            @Result(column = "userImg",property = "userImg"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "userTel",property = "userTel"),
            @Result(column = "userEmail",property = "userEmail"),
            @Result(column = "roleID",property = "roleId"),
            @Result(column = "roleID",property = "noteRole",one = @One(
                    select = "com.yang.dao.NoteRoleDao.queryByRID"
            )),
    },id = "noteUserMapper")
    @Select("SELECT * FROM noteUser")
    List<NoteUser> queryUser();

    /**
     * 根据用户编号获取用户信息 或者根据用户邮箱获取用户信息
     * @param userID
     * @return
     */
    @ResultMap("noteUserMapper")
    @Select("SELECT * FROM noteUser WHERE (userID=#{userID} OR userEmail =#{userID} OR username=#{userID})")
    NoteUser queryUserByID(String userID);

    /**
     * 用户登陆
     * @param username
     * @param password
     * @return
     */
    @Select("SELECT * FROM noteUser WHERE (username = #{username} or userEmail = #{username}) AND password = #{password}")
    @ResultMap("noteUserMapper")
    NoteUser userLogin(String username,String password);

    /**
     * 判断邮箱是否纯在
     * @param email
     * @return
     */
    @Select("SELECT COUNT(*) FROM noteUser WHERE userEmail = #{email}")
    Integer haveEmail(String email);

    /**
     * 超级管理员获取用户信息--总数
     * @return
     */
    @Select("SELECT * FROM noteUser " +
            "INNER JOIN userNoteInfo ON noteUser.userID = userNoteInfo.userID " +
            "ORDER BY noteResidue DESC")
    @Results(value = {
            @Result(column = "userID",property = "userid",id = true),
            @Result(column = "name",property = "name"),
            @Result(column = "userImg",property = "userImg"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "userTel",property = "userTel"),
            @Result(column = "userEmail",property = "userEmail"),
            @Result(column = "roleID",property = "roleId"),
            @Result(column = "roleID",property = "noteRole",one = @One(
                    select = "com.yang.dao.NoteRoleDao.queryByRID"
            )),
            @Result(column = "userID",property = "userNoteInfo",one = @One(
                    select = "com.yang.dao.UserNoteInfoDao.queryUserNoteByUid"
            ))
    },id = "superAdminMapper")
    List<NoteUser> superAdminQueryAll();

    /**
     * 分页获取用户信息
     * @return
     */
    @Select("SELECT * FROM noteUser " +
            "INNER JOIN userNoteInfo ON noteUser.userID = userNoteInfo.userID " +
            "ORDER BY noteResidue DESC LIMIT #{startPage},#{pageSize}")
    @ResultMap("superAdminMapper")
    List<NoteUser> superAdminQueryUserByPage(PageInfo pageInfo);

    /**
     * 超级管理员通过用户邮箱获取用户信息
     * @param email
     * @return
     */
    @Select("SELECT * FROM noteUser WHERE userEmail = #{email};")
    @ResultMap("superAdminMapper")
    NoteUser superAdminQueryUserByEmail(String email);
}